package cn.ibizlab.assetmanagement.assetapi.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.assetmanagement.assetapi.dto.*;
import cn.ibizlab.assetmanagement.assetapi.mapping.*;
import cn.ibizlab.assetmanagement.core.asset.domain.EAMDocument;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMDocumentService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMDocumentSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"文档" })
@RestController("AssetApi-eamdocument")
@RequestMapping("")
public class EAMDocumentResource {

    @Autowired
    public IEAMDocumentService eamdocumentService;

    @Autowired
    @Lazy
    public EAMDocumentMapping eamdocumentMapping;

    @PreAuthorize("hasPermission(this.eamdocumentMapping.toDomain(#eamdocumentdto),'iBizAssetManagement-EAMDocument-Create')")
    @ApiOperation(value = "新建文档", tags = {"文档" },  notes = "新建文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocuments")
    @Transactional
    public ResponseEntity<EAMDocumentDTO> create(@RequestBody EAMDocumentDTO eamdocumentdto) {
        EAMDocument domain = eamdocumentMapping.toDomain(eamdocumentdto);
		eamdocumentService.create(domain);
        EAMDocumentDTO dto = eamdocumentMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamdocumentMapping.toDomain(#eamdocumentdtos),'iBizAssetManagement-EAMDocument-Create')")
    @ApiOperation(value = "批量新建文档", tags = {"文档" },  notes = "批量新建文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocuments/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMDocumentDTO> eamdocumentdtos) {
        eamdocumentService.createBatch(eamdocumentMapping.toDomain(eamdocumentdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamdocument" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamdocumentService.get(#eamdocument_id),'iBizAssetManagement-EAMDocument-Update')")
    @ApiOperation(value = "更新文档", tags = {"文档" },  notes = "更新文档")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamdocuments/{eamdocument_id}")
    @Transactional
    public ResponseEntity<EAMDocumentDTO> update(@PathVariable("eamdocument_id") String eamdocument_id, @RequestBody EAMDocumentDTO eamdocumentdto) {
		EAMDocument domain  = eamdocumentMapping.toDomain(eamdocumentdto);
        domain .setEamdocumentid(eamdocument_id);
		eamdocumentService.update(domain );
		EAMDocumentDTO dto = eamdocumentMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamdocumentService.getEamdocumentByEntities(this.eamdocumentMapping.toDomain(#eamdocumentdtos)),'iBizAssetManagement-EAMDocument-Update')")
    @ApiOperation(value = "批量更新文档", tags = {"文档" },  notes = "批量更新文档")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamdocuments/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMDocumentDTO> eamdocumentdtos) {
        eamdocumentService.updateBatch(eamdocumentMapping.toDomain(eamdocumentdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamdocumentService.get(#eamdocument_id),'iBizAssetManagement-EAMDocument-Remove')")
    @ApiOperation(value = "删除文档", tags = {"文档" },  notes = "删除文档")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamdocuments/{eamdocument_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamdocument_id") String eamdocument_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamdocumentService.remove(eamdocument_id));
    }

    @PreAuthorize("hasPermission(this.eamdocumentService.getEamdocumentByIds(#ids),'iBizAssetManagement-EAMDocument-Remove')")
    @ApiOperation(value = "批量删除文档", tags = {"文档" },  notes = "批量删除文档")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamdocuments/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamdocumentService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamdocumentMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMDocument-Get')")
    @ApiOperation(value = "获取文档", tags = {"文档" },  notes = "获取文档")
	@RequestMapping(method = RequestMethod.GET, value = "/eamdocuments/{eamdocument_id}")
    public ResponseEntity<EAMDocumentDTO> get(@PathVariable("eamdocument_id") String eamdocument_id) {
        EAMDocument domain = eamdocumentService.get(eamdocument_id);
        EAMDocumentDTO dto = eamdocumentMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取文档草稿", tags = {"文档" },  notes = "获取文档草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamdocuments/getdraft")
    public ResponseEntity<EAMDocumentDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamdocumentMapping.toDto(eamdocumentService.getDraft(new EAMDocument())));
    }

    @ApiOperation(value = "检查文档", tags = {"文档" },  notes = "检查文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocuments/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMDocumentDTO eamdocumentdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamdocumentService.checkKey(eamdocumentMapping.toDomain(eamdocumentdto)));
    }

    @PreAuthorize("hasPermission(this.eamdocumentMapping.toDomain(#eamdocumentdto),'iBizAssetManagement-EAMDocument-Save')")
    @ApiOperation(value = "保存文档", tags = {"文档" },  notes = "保存文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocuments/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMDocumentDTO eamdocumentdto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamdocumentService.save(eamdocumentMapping.toDomain(eamdocumentdto)));
    }

    @PreAuthorize("hasPermission(this.eamdocumentMapping.toDomain(#eamdocumentdtos),'iBizAssetManagement-EAMDocument-Save')")
    @ApiOperation(value = "批量保存文档", tags = {"文档" },  notes = "批量保存文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocuments/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMDocumentDTO> eamdocumentdtos) {
        eamdocumentService.saveBatch(eamdocumentMapping.toDomain(eamdocumentdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMDocument-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMDocument-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"文档" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamdocuments/fetchdefault")
	public ResponseEntity<List<EAMDocumentDTO>> fetchDefault(EAMDocumentSearchContext context) {
        Page<EAMDocument> domains = eamdocumentService.searchDefault(context) ;
        List<EAMDocumentDTO> list = eamdocumentMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMDocument-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMDocument-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"文档" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamdocuments/searchdefault")
	public ResponseEntity<Page<EAMDocumentDTO>> searchDefault(@RequestBody EAMDocumentSearchContext context) {
        Page<EAMDocument> domains = eamdocumentService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamdocumentMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.eamdocumentMapping.toDomain(#eamdocumentdto),'iBizAssetManagement-EAMDocument-Create')")
    @ApiOperation(value = "根据资产类型建立文档", tags = {"文档" },  notes = "根据资产类型建立文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/{eamassettype_id}/eamdocuments")
    @Transactional
    public ResponseEntity<EAMDocumentDTO> createByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @RequestBody EAMDocumentDTO eamdocumentdto) {
        EAMDocument domain = eamdocumentMapping.toDomain(eamdocumentdto);
        domain.setEamassettypeid(eamassettype_id);
		eamdocumentService.create(domain);
        EAMDocumentDTO dto = eamdocumentMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamdocumentMapping.toDomain(#eamdocumentdtos),'iBizAssetManagement-EAMDocument-Create')")
    @ApiOperation(value = "根据资产类型批量建立文档", tags = {"文档" },  notes = "根据资产类型批量建立文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/{eamassettype_id}/eamdocuments/batch")
    public ResponseEntity<Boolean> createBatchByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @RequestBody List<EAMDocumentDTO> eamdocumentdtos) {
        List<EAMDocument> domainlist=eamdocumentMapping.toDomain(eamdocumentdtos);
        for(EAMDocument domain:domainlist){
            domain.setEamassettypeid(eamassettype_id);
        }
        eamdocumentService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamdocument" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamdocumentService.get(#eamdocument_id),'iBizAssetManagement-EAMDocument-Update')")
    @ApiOperation(value = "根据资产类型更新文档", tags = {"文档" },  notes = "根据资产类型更新文档")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassettypes/{eamassettype_id}/eamdocuments/{eamdocument_id}")
    @Transactional
    public ResponseEntity<EAMDocumentDTO> updateByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @PathVariable("eamdocument_id") String eamdocument_id, @RequestBody EAMDocumentDTO eamdocumentdto) {
        EAMDocument domain = eamdocumentMapping.toDomain(eamdocumentdto);
        domain.setEamassettypeid(eamassettype_id);
        domain.setEamdocumentid(eamdocument_id);
		eamdocumentService.update(domain);
        EAMDocumentDTO dto = eamdocumentMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamdocumentService.getEamdocumentByEntities(this.eamdocumentMapping.toDomain(#eamdocumentdtos)),'iBizAssetManagement-EAMDocument-Update')")
    @ApiOperation(value = "根据资产类型批量更新文档", tags = {"文档" },  notes = "根据资产类型批量更新文档")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassettypes/{eamassettype_id}/eamdocuments/batch")
    public ResponseEntity<Boolean> updateBatchByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @RequestBody List<EAMDocumentDTO> eamdocumentdtos) {
        List<EAMDocument> domainlist=eamdocumentMapping.toDomain(eamdocumentdtos);
        for(EAMDocument domain:domainlist){
            domain.setEamassettypeid(eamassettype_id);
        }
        eamdocumentService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamdocumentService.get(#eamdocument_id),'iBizAssetManagement-EAMDocument-Remove')")
    @ApiOperation(value = "根据资产类型删除文档", tags = {"文档" },  notes = "根据资产类型删除文档")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassettypes/{eamassettype_id}/eamdocuments/{eamdocument_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @PathVariable("eamdocument_id") String eamdocument_id) {
		return ResponseEntity.status(HttpStatus.OK).body(eamdocumentService.remove(eamdocument_id));
    }

    @PreAuthorize("hasPermission(this.eamdocumentService.getEamdocumentByIds(#ids),'iBizAssetManagement-EAMDocument-Remove')")
    @ApiOperation(value = "根据资产类型批量删除文档", tags = {"文档" },  notes = "根据资产类型批量删除文档")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassettypes/{eamassettype_id}/eamdocuments/batch")
    public ResponseEntity<Boolean> removeBatchByEAMAssetType(@RequestBody List<String> ids) {
        eamdocumentService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamdocumentMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMDocument-Get')")
    @ApiOperation(value = "根据资产类型获取文档", tags = {"文档" },  notes = "根据资产类型获取文档")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassettypes/{eamassettype_id}/eamdocuments/{eamdocument_id}")
    public ResponseEntity<EAMDocumentDTO> getByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @PathVariable("eamdocument_id") String eamdocument_id) {
        EAMDocument domain = eamdocumentService.get(eamdocument_id);
        EAMDocumentDTO dto = eamdocumentMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据资产类型获取文档草稿", tags = {"文档" },  notes = "根据资产类型获取文档草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/eamassettypes/{eamassettype_id}/eamdocuments/getdraft")
    public ResponseEntity<EAMDocumentDTO> getDraftByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id) {
        EAMDocument domain = new EAMDocument();
        domain.setEamassettypeid(eamassettype_id);
        return ResponseEntity.status(HttpStatus.OK).body(eamdocumentMapping.toDto(eamdocumentService.getDraft(domain)));
    }

    @ApiOperation(value = "根据资产类型检查文档", tags = {"文档" },  notes = "根据资产类型检查文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/{eamassettype_id}/eamdocuments/checkkey")
    public ResponseEntity<Boolean> checkKeyByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @RequestBody EAMDocumentDTO eamdocumentdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamdocumentService.checkKey(eamdocumentMapping.toDomain(eamdocumentdto)));
    }

    @PreAuthorize("hasPermission(this.eamdocumentMapping.toDomain(#eamdocumentdto),'iBizAssetManagement-EAMDocument-Save')")
    @ApiOperation(value = "根据资产类型保存文档", tags = {"文档" },  notes = "根据资产类型保存文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/{eamassettype_id}/eamdocuments/save")
    public ResponseEntity<Boolean> saveByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @RequestBody EAMDocumentDTO eamdocumentdto) {
        EAMDocument domain = eamdocumentMapping.toDomain(eamdocumentdto);
        domain.setEamassettypeid(eamassettype_id);
        return ResponseEntity.status(HttpStatus.OK).body(eamdocumentService.save(domain));
    }

    @PreAuthorize("hasPermission(this.eamdocumentMapping.toDomain(#eamdocumentdtos),'iBizAssetManagement-EAMDocument-Save')")
    @ApiOperation(value = "根据资产类型批量保存文档", tags = {"文档" },  notes = "根据资产类型批量保存文档")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/{eamassettype_id}/eamdocuments/savebatch")
    public ResponseEntity<Boolean> saveBatchByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @RequestBody List<EAMDocumentDTO> eamdocumentdtos) {
        List<EAMDocument> domainlist=eamdocumentMapping.toDomain(eamdocumentdtos);
        for(EAMDocument domain:domainlist){
             domain.setEamassettypeid(eamassettype_id);
        }
        eamdocumentService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMDocument-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMDocument-Get')")
	@ApiOperation(value = "根据资产类型获取DEFAULT", tags = {"文档" } ,notes = "根据资产类型获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamassettypes/{eamassettype_id}/eamdocuments/fetchdefault")
	public ResponseEntity<List<EAMDocumentDTO>> fetchEAMDocumentDefaultByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id,EAMDocumentSearchContext context) {
        context.setN_eamassettypeid_eq(eamassettype_id);
        Page<EAMDocument> domains = eamdocumentService.searchDefault(context) ;
        List<EAMDocumentDTO> list = eamdocumentMapping.toDto(domains.getContent());
	    return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMDocument-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMDocument-Get')")
	@ApiOperation(value = "根据资产类型查询DEFAULT", tags = {"文档" } ,notes = "根据资产类型查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamassettypes/{eamassettype_id}/eamdocuments/searchdefault")
	public ResponseEntity<Page<EAMDocumentDTO>> searchEAMDocumentDefaultByEAMAssetType(@PathVariable("eamassettype_id") String eamassettype_id, @RequestBody EAMDocumentSearchContext context) {
        context.setN_eamassettypeid_eq(eamassettype_id);
        Page<EAMDocument> domains = eamdocumentService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamdocumentMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

